

package cn.shengchao.examstar.distribute.infrastrure.service.impl;

import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeDataStatusEnum;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeSaleRoleEnum;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeTypeEnum;
import cn.shengchao.examstar.rpc.distribute.req.DistributeNodeInfoQueryPage;
import cn.shengchao.examstar.rpc.distribute.resp.DistributeNodeInfoRespEntity;
import cn.shengchao.examstar.rpc.uddsys.resp.DistributeUserInviteSelectVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.util.StrUtil;
import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeNodeInfoMapper;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeInfo;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeNodeInfoService;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class DistributeNodeInfoServiceImpl extends ServiceImpl<DistributeNodeInfoMapper, DistributeNodeInfo> implements DistributeNodeInfoService {

    @Override
    public DistributeNodeInfo getNodeInfoForUser(Long userId, DistributeTypeEnum distributeTypeEnum) {
        DistributeNodeInfo distributeNodeInfo = getOne(Wrappers.lambdaQuery(DistributeNodeInfo.class)
                .eq(DistributeNodeInfo::getUserId, userId)
                .eq(DistributeNodeInfo::getDistributeType, distributeTypeEnum.getType())
                .eq(DistributeNodeInfo::getDataStatus, DistributeDataStatusEnum.STATUS_CURT.getStatus()));
        return distributeNodeInfo;
    }

    /**
     * 根据用户id获取分销节点信息
     *
     * @param userId 用户id
     * @return map key为用户id, value为分销节点信息
     */
    @Override
    public Map<Long, Optional<DistributeNodeInfo>> getDistributeNodeInfo(List<Long> userId) {
        if (userId.isEmpty()) {
            return Map.of();
        }
        List<DistributeNodeInfo> distributeNodeInfo = baseMapper.selectList(Wrappers
                .lambdaQuery(DistributeNodeInfo.class)
                .in(DistributeNodeInfo::getUserId, userId));
        return distributeNodeInfo.stream()
                .collect(Collectors.groupingBy(DistributeNodeInfo::getUserId, Collectors.reducing((a, b) -> b)));
    }

    @Override
    public DistributeNodeInfo getDistributeNodeInfo(Long userId) {
        if (userId != null) {
            return baseMapper.selectOne(Wrappers.lambdaQuery(DistributeNodeInfo.class)
                    .eq(DistributeNodeInfo::getUserId, userId).last("limit 1"));
        }
        return null;
    }

    @Override
    public DistributeNodeInfo getDistributeNodeInfoInCode(Long userId, List<DistributeSaleRoleEnum> distributeSaleRoleEnums) {
        if (userId != null) {
            return baseMapper.selectOne(Wrappers.lambdaQuery(DistributeNodeInfo.class)
                    .eq(DistributeNodeInfo::getUserId, userId)
                    .in(DistributeNodeInfo::getDistributeRoleCode, distributeSaleRoleEnums.stream().map(DistributeSaleRoleEnum::getCode).collect(Collectors.toSet()))
                    .last("limit 1")
            );
        }
        return null;
    }

    @Override
    public DistributeNodeInfo getDistributeNodeInfo(Long userId, DistributeSaleRoleEnum distributeSaleRoleEnum) {
        if (userId != null) {
            return baseMapper.selectOne(Wrappers.lambdaQuery(DistributeNodeInfo.class)
                    .eq(DistributeNodeInfo::getUserId, userId)
                    .eq(DistributeNodeInfo::getDistributeRoleCode, distributeSaleRoleEnum.getCode())
                    .last("limit 1")
            );
        }
        return null;
    }

    @Override
    public void updateProfile(Long userId, String headImageUrl, String fullName, DistributeSaleRoleEnum role) {
        DistributeNodeInfo distributeNodeInfo = baseMapper.selectOne(Wrappers.lambdaQuery(DistributeNodeInfo.class)
                .eq(DistributeNodeInfo::getUserId, userId)
                .eq(DistributeNodeInfo::getDistributeRoleCode, role.getCode())
                .last("limit 1"));
        if (distributeNodeInfo != null) {
            if (StrUtil.isNotBlank(headImageUrl)) {
                distributeNodeInfo.setHeadImageUrl(headImageUrl);
            }
            if (StrUtil.isNotBlank(fullName)) {
                distributeNodeInfo.setFullName(fullName);
            }
            baseMapper.updateById(distributeNodeInfo);
        }
    }

    @Override
    public void updateEmployeeStatus(Long userId, Integer status) {
        DistributeNodeInfo distributeNodeInfo = baseMapper.selectOne(Wrappers.lambdaQuery(DistributeNodeInfo.class)
                .eq(DistributeNodeInfo::getUserId, userId));
        if (distributeNodeInfo != null) {
            distributeNodeInfo.setDataStatus(status);
            baseMapper.updateById(distributeNodeInfo);
        }
    }

    @Override
    public List<DistributeUserInviteSelectVo> selectDistributeUserInviteSelectVo(String searchValue) {
        return baseMapper.selectDistributeUserInviteSelectVo(searchValue);
    }

    @Override
    public PageResult<DistributeNodeInfoRespEntity> selectDistributeNodesByPage(DistributeNodeInfoQueryPage pageQuery) {
        Page<Object> page = PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<DistributeNodeInfoRespEntity> nodeInfoList = baseMapper.selectDistributeNodesByPage(pageQuery);
        return PageResult.of(page.getTotal(), nodeInfoList);
    }
}
